package com.zzu.peifuyang.socketclient;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.icu.util.Output;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {

    private EditText sno_number,sno_name,sno_phone,sno_qq,sno_class;
    RadioButton major;
    private Button submit;
    Register register;
    OutputStream os;
    PrintWriter pw;
    private String serverText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_view();
//        初始化套接字




    }
    public void init_view(){
        sno_number = findViewById(R.id.sno_number);
        sno_name = findViewById(R.id.sno_name);
        sno_phone = findViewById(R.id.sno_phone);
        sno_qq = findViewById(R.id.sno_qq);
        sno_class = findViewById(R.id.sno_class);
        major = findViewById(R.id.majored);
        submit = findViewById(R.id.submit);
        register = new Register();
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register.setSno_number(sno_number.getText().toString());
                register.setSno_name(sno_name.getText().toString());
                register.setSno_phone(sno_phone.getText().toString());
                register.setSno_qq(sno_qq.getText().toString());
                register.setSno_class(sno_class.getText().toString());
                register.setMajor(major.isChecked());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        go();
                    }
                }).start();
                try {
                    postMessage(register);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });
    }
    public void postMessage(final Register register){
        Runnable post = new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void run() {
                try {
                    Socket socket = new Socket("192.168.0.100", 8848);
                    os = socket.getOutputStream();
                    pw = new PrintWriter(os);
                    pw.write(register.toString());
                    pw.flush();
                    Scanner sc  = new Scanner(socket.getInputStream());

                    while (!socket.isClosed()){
                        Thread.sleep(300);
                        if(sc.hasNextLine()){
                            serverText = sc.nextLine();
                            testNotify();
                            break;
                        }
                    }
                    socket.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        ThreadTester tester = new ThreadTester(post);


    }
    public void joinGroup(View view){
            Intent intent = new Intent();
            intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D" +"VPNyYBDcJ-GT2H_pEOLZ4JEjzzi4kdV3"));
            startActivity(intent);
        }
     @RequiresApi(api = Build.VERSION_CODES.O)
     public void testNotify(){


             NotificationManager notificationManager =  (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
             Notification notification = null;

             String channelId = String.valueOf("speechId");
             CharSequence channelName = "English speech";
             String channelDescription = "notify channel of English speech ";

             int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;

             NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
             // 设置描述 最长30字符



             notificationManager.createNotificationChannel(notificationChannel);
             NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
//             设置标题
             notification = mBuilder.setChannelId(channelId)
                     .setContentTitle("英语演讲通知")
                     .setContentText(serverText)
                     .setSmallIcon(R.mipmap.ic_launcher_round).build();
//             发送通知
             notificationManager.notify(1024,notification);

     }
     public void go(){
        Intent i = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(i);
     }

}

